// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
  const data = []
  let orderList

  if(event.status == ""){
    orderList = await findAllOrder(event)
  }else{
    orderList = await findAllOrderByStatus(event)
  }
  
  for(let i = 0; i < orderList.data.length; i++){
    let order = {}
    order.student_id = orderList.data[i].student_id
    order.student_name = orderList.data[i].student_name
    order.student_phone = orderList.data[i].student_phone
    order.id = orderList.data[i]._id
    order.time = orderList.data[i].order_time
    order.goodsName = orderList.data[i].meal_name
    order.price = orderList.data[i].meal_price
    order.status = orderList.data[i].order_status
    order.num = orderList.data[i].order_random
    order.meal_img = orderList.data[i].meal_img
    data.push(order)
  }
  return data;
}

async function findAllOrder (event){
  return db.collection('order_info').where({
    canteen_id: event.canteen_id,
    window_id: event.window_id
  }).get()
}
async function findAllOrderByStatus (event){
  return db.collection('order_info').where({
    canteen_id: event.canteen_id,
    window_id: event.window_id,
    order_status: event.status
  }).get()
}
